/*
The MIT License (MIT)
Copyright (c) 2013 Dalvik Boss dalvikboss@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package sidl.generator.c;

import sidl.Clazz;
import sidl.Const;
import sidl.Field;
import sidl.Func;
import sidl.generator.Generator;
import sidl.generator.GeneratorContext;

public class AnsicHeaderGenerator extends Generator{

    public AnsicHeaderGenerator(GeneratorContext context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onGenerate() throws Throwable {

        println("#ifndef _%s_%s_H_", doc.packageName.toUpperCase().replace('.', '_'), doc.mainInf().name.toUpperCase());
        println("#define _%s_%s_H_", doc.packageName.toUpperCase().replace('.', '_'), doc.mainInf().name.toUpperCase());
        generateConsts();
        generateClazzStructProtoTypes();
        generateBasicTypes();
        generateClazzStructs();
        new FunctionTypeGenerator(context).generate();
        
        generateFunctionProtoTypes();
        println("#endif//_%s_%s_H_", doc.packageName.toUpperCase().replace('.', '_'), doc.mainInf().name.toUpperCase());
    }


    private void generateConsts() {
        for(Const cs : doc.mainInf().consts) {
            println("#define %s %s", cs.name, cs.value);
        }
        
    }

    private void generateFunctionProtoTypes() {
        for(Func f: doc.mainInf().funcs) {
            String pointer = "";
            if(f.rtype.isArray || f.rtype.isClazz()) {
                pointer = "*";
            }
            if(f.params.size()==0) {
                println("%s %s%s(agent_context *agentctx);", f.rtype.toC(), pointer,  c_function_name(f));    
            } else {
                println("%s %s%s(agent_context *agentctx, %s);", f.rtype.toC(), pointer,  c_function_name(f),  c_function_formal_argslist(f));
            }
            
        }
        
        println("void memdump_of_%s_%s(io_context *ioctx);\n", doc.cPackageName(), doc.mainInf().name);
        println("call_status clean_of_%s_%s(agent_context *agentctx);", doc.cPackageName(), doc.mainInf().name);
        
        
        
    }

    protected void generateClazzStructProtoTypes() throws Throwable {
        for(Clazz clz: doc.clazzes) {
            println("typedef struct %s %s;", clz.name, clz.name);
        }
        for(Clazz clz: doc.clazzes) {
            println("typedef struct %s_array %s_array;", clz.name, clz.name);
        }
        println("typedef struct cstring cstring;");
        println("typedef struct cstring_array cstring_array;");



        
       
    }
    
    protected void generateClazzStructs() throws Throwable {
        for(Clazz clz: doc.clazzes) {
            println("struct %s {", clz.name);
            for(Field f: clz.fields) {
                println("    %s %s;",f.type.asReturnTypeOfC(),  f.name);
            }
            println("};");
            
            println("struct %s_array {", clz.name);
            println("    int size;");
            println("    %s* *elems;", clz.name);
            println("};");
            
        }
        
        println("struct cstring{");
        println("    int size;");
        println("    char *data;");
        println("};");
        
        println("struct cstring_array{");
        println("    int size;");
        println("    cstring* *elems;");
        println("};");

        
    }
    
    private void generateBasicTypes() {

        println("typedef struct io_context {");
        println("    int fd;");
        println("    struct {");
        println("        int total;");
        println("        int used;");
        println("        struct {");
        println("#define MEM_BLOCKS_MAX 8192");
        println("#define BUFF_MAX %d", doc.getBufferSize());
        println("#ifdef MMDBG");
        println("            const char *filename;");
        println("            int lineno;");
        println("            const char *function;");
        println("#endif");
        println("            int size;");
        println("            void *p;");
        println("        }blocks[MEM_BLOCKS_MAX];");
        println("    }mem;");
        println("struct {");
        println("    int pos;");
        println("    char data[BUFF_MAX];");
        println("}buff;");
        
        println("}io_context;");

        println("typedef enum call_status {");
        println("    ct_failed = 0,");
        println("    ct_ok");
        println("}call_status;");
        
        println("typedef struct agent_context {");
        if(doc.isAndroidIpc()) {
            println("    char *name;");
        } else {
            println("    char *ip;");
            println("    int port;");
            
        }
        println("    io_context ioctx;");
        println("    call_status status;//");
        println("    RemoteThrowable *throwable;");

        
        println("}agent_context;");
        
        
        println("typedef struct int_array {");
        println("    int size;");
        println("    int *elems;");
        println("}int_array;");
    }

}
